package com.yanqu.road.dao.impl.activity.rankunionwar;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.rankunionwar.data.CrossRankUnionWarData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CrossRankUnionWarDataDaoImpl extends TempDataObjectDao<CrossRankUnionWarData> {

        public static String getDataSql = "select * from t_c_activity_rank_union_war where activityId=?";
    public CrossRankUnionWarData getData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getDataSql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossRankUnionWarData data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertUserDataSql = "insert into t_c_activity_rank_union_war(" +
            " activityId,gameTurn,isUnionGet,battleUnionIdData) " +
            " values (?,?,?,?)";
    @Override
    protected boolean addData(CrossRankUnionWarData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGameTurn()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isUnionGet()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getBattleUnionIdMap())));

        if (getDb().executeNoneQuery(insertUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUserDataSql = "update t_c_activity_rank_union_war set " +
            " gameTurn=?,isUnionGet=?,battleUnionIdData=?" +
            " where activityId=?";
    @Override
    protected boolean updateData(CrossRankUnionWarData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getGameTurn()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isUnionGet()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getBattleUnionIdMap())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        if (getDb().executeNoneQuery(updateUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossRankUnionWarData parseData(ResultSet rs) throws SQLException {
        CrossRankUnionWarData data = new CrossRankUnionWarData();
        data.setActivityId(rs.getInt("activityId"));
        data.setGameTurn(rs.getInt("gameTurn"));
        data.setUnionGet(rs.getBoolean("isUnionGet"));

        String battleUnionIdDataStr  = rs.getString("battleUnionIdData");
        if(!StringUtils.isNullOrEmpty(battleUnionIdDataStr)){
            Map<Integer, Map<Integer, List<String>>> result = JSON.parseObject(battleUnionIdDataStr, new TypeReference<Map<Integer, Map<Integer, List<String>>>>() {});
            if(result != null){
                data.setBattleUnionIdMap(result);
            }
        }
        return data;
    }

}
